/*
 * =====================================================================================
 *
 *       Filename:  Mediator.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月04日 20时40分38秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  LiuYi (), swliuyi@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <limits.h>
#include <iostream>

using namespace std;

class Colleague;
class Mediator
{
    public:
        virtual void Send(string message, Colleague * colleague) = 0;
        virtual ~ Mediator(){}
};

class Colleague
{
    protected:
        Mediator * m_mediator;
    public:
        Colleague(Mediator * mediator):m_mediator(mediator){}
        virtual ~ Colleague()
        {
            delete m_mediator;
        }
};

class ConcreteColleague1 : public Colleague
{
    public:
        ConcreteColleague1(Mediator * mediator):Colleague(mediator){}
        void Send(string message)
        {
            m_mediator->Send(message, this);
        }
        void Notify(string message)
        {
            cout<<"Colleague1 receive the message: "<<message<<endl;
        }
};

class ConcreteColleague2 : public Colleague
{
    public:
        ConcreteColleague2(Mediator * mediator):Colleague(mediator){}
        void Send(string message)
        {
            m_mediator->Send(message, this);
        }
        void Notify(string message)
        {
            cout<<"Colleague2 receive the message: "<<message<<endl;
        }
};


class ConcreteMediator : public Mediator
{
    private:
        ConcreteColleague1 * m_colleague1;
        ConcreteColleague2 * m_colleague2;
    public:
        void SetColleague1(ConcreteColleague1 * colleague)
        {
            m_colleague1 = colleague;
        }
        void SetColleague2(ConcreteColleague2 * colleague)
        {
            m_colleague2 = colleague;
        }
        void Send(string message, Colleague * colleague)
        {
            if( colleague == m_colleague1 )
            {
                m_colleague2->Notify(message);
            }
            else if( colleague == m_colleague2 )
            {
                m_colleague1->Notify(message);
            }
            else
            {
                cout<<"colleague has not log in"<<endl;
            }
        }
        virtual ~ ConcreteMediator()
        {
            delete m_colleague1;
            delete m_colleague2;
        }
};


int main(int argc, char *argv[])
{

    ConcreteMediator * cm = new ConcreteMediator();

    ConcreteColleague1 * cc1 = new ConcreteColleague1(cm);
    ConcreteColleague2 * cc2 = new ConcreteColleague2(cm);

    cm->SetColleague1(cc1);
    cm->SetColleague2(cc2);

    cc1->Send("have you eaten?");
    cc2->Send("No");

    delete cm;
}
